package com.cos.lession73.slice;

import com.cos.lession73.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;

public class LongClickAbilitySlice extends AbilitySlice {

    boolean conFlag = false;
    boolean favoriteFlag = false;
    boolean likeFlag = false;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_longclick);

        Image favorite;
        favorite = (Image) findComponentById(ResourceTable.Id_bili_favorite_img);
        Image con;
        con = (Image) findComponentById(ResourceTable.Id_bili_con_img);
        Image like;
        like = (Image) findComponentById(ResourceTable.Id_bili_like_img);
        like.setLongClickedListener(new Component.LongClickedListener() {
            @Override
            public void onLongClicked(Component component) {
                like.setImageAndDecodeBounds(ResourceTable.Media_bili_Like);
                con.setImageAndDecodeBounds(ResourceTable.Media_bili_pay_con);
                favorite.setImageAndDecodeBounds(ResourceTable.Media_bili_favorite);
                //设置状态
                conFlag = true;
                favoriteFlag = true;
                likeFlag = true;
            }
        });
        like.setClickedListener(this::onClick);
        favorite.setClickedListener(this::onClick);
        con.setClickedListener(this::onClick);


    }

    public void onClick(Component component) {
        Image img = (Image) component;
        //判断及设置对应组件的图片资源id
        switch (component.getId()){
            case ResourceTable.Id_bili_like_img:
                //设置选中的图片的id号进行赋值
                if (likeFlag) {
                    img.setImageAndDecodeBounds(ResourceTable.Media_bili_unLike);
                    likeFlag=false;
                }else{
                    img.setImageAndDecodeBounds(ResourceTable.Media_bili_Like);
                    likeFlag=true;
                }
                break;
            case ResourceTable.Id_bili_con_img:
                //设置选中的图片的id号进行赋值
                if (conFlag) {
                    img.setImageAndDecodeBounds(ResourceTable.Media_bili_unpay_con);
                    conFlag=false;
                }else{
                    img.setImageAndDecodeBounds(ResourceTable.Media_bili_pay_con);
                    conFlag=true;
                }
                break;
            case ResourceTable.Id_bili_favorite_img:
                //设置选中的图片的id号进行赋值
                if (favoriteFlag) {
                    img.setImageAndDecodeBounds(ResourceTable.Media_bili_unfavorite);
                    favoriteFlag=false;
                }else{
                    img.setImageAndDecodeBounds(ResourceTable.Media_bili_favorite);
                    favoriteFlag=true;
                }
                break;
        }


    }
}
